package top.livepan.system.controller;

import com.livepan.server.BaseController;
import com.livepan.server.error.BusinessException;
import com.livepan.server.response.CommonReturnType;
import com.livepan.server.service.RoleService;
import com.livepan.server.service.model.RoleModel;
import com.livepan.server.service.model.PageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {

    public static final String BUSINESS_NAME = "角色";

    private static final Logger LOG = LoggerFactory.getLogger(RoleController.class);

    @Autowired
    private RoleService roleService;

    @ResponseBody
    @RequestMapping("/list")
    public CommonReturnType role(PageModel<RoleModel> pageModel) {
        LOG.info("page:" + pageModel.getPage());
        roleService.list(pageModel);
        return CommonReturnType.create(pageModel);
    }

    @ResponseBody
    @RequestMapping("/allRole")
    public CommonReturnType allRole() {
        List<RoleModel> roleModelList = roleService.allRole();
        return CommonReturnType.create(roleModelList);
    }

    @ResponseBody
    @RequestMapping("/save")
    public CommonReturnType saveRole(@RequestBody RoleModel roleModel) throws BusinessException {
        roleService.save(roleModel);
        return CommonReturnType.create(roleModel);
    }

    @ResponseBody
    @RequestMapping("/delete/{id}")
    public CommonReturnType delete(@PathVariable String id) {
        roleService.delete(id);
        return CommonReturnType.create(null);
    }
}
